package com.kcral.javase.ch.properties;

/**
 * @TODO
 * @Author xu
 * 2025/3/4
 */
public class Persons {
    private String name;
    private int age;
    private boolean gender;  // true for male, false for female
    private Person partner;   // spouse

    public Persons(String name, int age, boolean gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.partner = null; // Initially, no partner
    }

    public boolean isMarried() {
        return partner != null;
    }

    public void marry(Person p) {
        // Check if the marriage can happen
        if (this.gender == p.gender) {
            System.out.println("不能结婚：同性之间不能结婚。");
        } else if ((this.gender && this.age < 24) || (!this.gender && this.age < 22)) {
            System.out.println("不能结婚：未达到结婚年龄。");
        } else if (this.isMarried() || p.isMarried()) {
            System.out.println("不能结婚：某一方已婚。");
        } else {
            // Perform the marriage
            this.partner = p;
            p.partner = this; // Set the partner for the other person
            System.out.println(this.name + " 和 " + p.name + " 恭喜结婚！");
        }
    }

    // Getters for testing
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public boolean isGender() {
        return gender;
    }

    public Person getPartner() {
        return partner;
    }
}
